package com.mediacloud.app.newsmodule.activity.audio;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.newsmodule.R;

public class AudioAlbumProgramWinX extends PopupWindow {

    protected View contentView;
    protected Context mContext;
    private RecyclerView recyclerView;


    public AudioAlbumProgramWinX(Context context) {
        super(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new BitmapDrawable());
        setOutsideTouchable(true);
        mContext = context;

        contentView = LayoutInflater.from(mContext).inflate(R.layout.aduio_album_program_list_win, null);
        setContentView(contentView);
        contentView.setOnTouchListener((v, event) -> {
            boolean b = contentView.onTouchEvent(event);
            dismiss();
            return b;
        });

        recyclerView = contentView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        setAnimationStyle(R.style.BottomDialog_Animation2);
    }

    public void setAdaptor(AudioAlbumProgramAdapter adaptor) {

        recyclerView.setAdapter(adaptor);
    }



    public void show(final View view) {
        dismiss();
        int heightPixels = mContext.getResources().getDisplayMetrics().heightPixels;
        if (contentView.getHeight() <= 0) {
            contentView.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    contentView.getViewTreeObserver().removeOnPreDrawListener(this);
                    dismiss();
                    int heightPixels = mContext.getResources().getDisplayMetrics().heightPixels;
                    showAtLocation(view, Gravity.NO_GRAVITY, 0, heightPixels - contentView.getHeight());
                    return true;
                }
            });
        }
        showAtLocation(view, Gravity.NO_GRAVITY, 0, heightPixels - contentView.getHeight());
    }

    public void show(View view, boolean showTitle) {
        contentView.findViewById(R.id.frame_title).setVisibility(showTitle ? View.VISIBLE : View.GONE);
        show(view);
    }
}